{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import fasttext"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Read 399M words\n",
      "Number of words:  9931866\n",
      "Number of labels: 3\n",
      "Progress: 100.0% words/sec/thread: 5073683 lr:  0.000000 avg.loss:  0.026757 ETA:   0h 0m 0s 19.0% words/sec/thread: 5129594 lr:  0.080970 avg.loss:  0.057336 ETA:   0h 0m16s avg.loss:  0.026757 ETA:   0h 0m 0s\n"
     ]
    }
   ],
   "source": [
    "model = fasttext.train_supervised('shuf-train-fasttext-bahasa-en.txt', dim = 16, minn = 2, loss = 'hs')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.save_model(\"model-bahasa-en.bin\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# model = fasttext.load_model('model-bahasa-en.bin')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "cutoff = int(len(model.get_words()) * 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 46 µs, sys: 1 µs, total: 47 µs\n",
      "Wall time: 49.1 µs\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(('__label__bahasa', '__label__english'),\n",
       " array([9.99039471e-01, 9.80577781e-04]))"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "model.predict(\"bodo siak\", k = 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 26 µs, sys: 1e+03 ns, total: 27 µs\n",
      "Wall time: 27.2 µs\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(('__label__bahasa',), array([1.00001001]))"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "model.predict(\"rada ketuaan kalo umur masih pas lebih bagus lg aishwarya\", k = 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 19 µs, sys: 0 ns, total: 19 µs\n",
      "Wall time: 20.3 µs\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(('__label__english', '__label__bahasa', '__label__other'),\n",
       " array([9.87883389e-01, 1.21458061e-02, 1.05853214e-05]))"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "model.predict(\"stupid shit\", k = 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# model.quantize(input='shuf-train-fasttext-bahasa-en.txt', retrain=True, qnorm=True, cutoff = cutoff)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.quantize(qnorm=True, cutoff = cutoff)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.save_model(\"model-bahasa-en.ftz\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!cp model-bahasa-en.bin ~/ssd3/fasttext-language-detection-bahasa-en/fasttext.bin\n",
    "!cp model-bahasa-en.ftz ~/ssd3/fasttext-language-detection-bahasa-en/fasttext.ftz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
